import * as types from './types'

export default {
  [types.NEXTSONG](state) {},
  [types.SENDAUDIO](state, obj) {
    state.dom = obj
  },
  [types.SENDMUSIC](state, obj) {
    // state.playState = true
    let addFlag = true;
    for (let i=0;i<state.musicList.length;i++){
      if (state.musicList[i].songId === obj.songId){
        addFlag = false;
      }
      
    }
    if (addFlag){
      state.musicList.push(obj)
      //只添加歌曲到播放列表，不切换
      if (state.musicPlace === -1){
        state.musicPlace = state.musicList.length - 1
        state.dom.src =
          state.musicList[state.musicPlace].src
      }
      
    }
    
  },
  [types.SENDANDCHANGE](state, obj) {
    // state.playState = true
    let addFlag = true;
    let findIndex = state.musicList.length;
    for (let i=0;i<state.musicList.length;i++){
      //string == num
      if (state.musicList[i].songId == obj.songId){
        addFlag = false;
        findIndex = i;
      }
      
    }
    if (addFlag){
      state.musicList.push(obj)
    }
      
    state.musicPlace = findIndex
    state.dom.src =
      state.musicList[state.musicPlace].src
      
      
    
    
  },
  [types.LOOPPLAYBACK](state) {
    if (state.musicPlace >= state.musicList.length - 1) {
      state.musicPlace = 0
    } else {
      state.musicPlace++
    }
    state.dom.src =
      state.musicList[state.musicPlace].src
    if (state.playState){
      state.dom.play()
    }
     
  },
  [types.PREVSONG](state) {
    if (state.musicPlace == 0) {
      state.musicPlace = state.musicList.length - 1
    } else {
      state.musicPlace--
    }
    state.dom.src =
      state.musicList[state.musicPlace].src
    if (state.playState){
      state.dom.play()
    }
  },
  [types.CHANGEISTHEMEDARK]: (state,val) =>
    (state.is_theme_dark = val),
 
  [types.PAUSE](state) {
    state.playState = false
  },
  [types.PLAY](state) {
    state.playState = true
    
  },
  [types.CLOSELISTSTATE](state) {
    state.listState = false
  },
  [types.OPENLISTSTATE](state) {
    state.listState = true
  },
  [types.SELECTMUSIC](state, index) {
    state.musicPlace = index
    state.dom.src =
      state.musicList[state.musicPlace].src 
    if (state.playState){
      state.dom.play()
    }
      
  },

  [types.DELMUSIC](state, index) {
    index <= state.musicPlace ? state.musicPlace-- : false
    
    state.musicList.splice(index, 1)
    if (state.musicPlace === -1 && state.musicList.length){
      state.musicPlace = 0;
    }
    if (state.musicPlace === -1){
      state.dom.src = "";
      state.playState = false;
    }
    else if (state.dom.src !=state.musicList[state.musicPlace].src){
      state.dom.src = state.musicList[state.musicPlace].src 
    }
    
    if (state.playState){
      state.dom.play()
    }
  },
  [types.SETCURRENTTIME](state,time){
    state.dom.currentTime = time;
  },
  [types.SETLYRICS](state,value){
    state.lyric = value;
  },
  [types.SETLYRICSARR](state,arr){
    state.lyricsArr = arr;
  },
  [types.SETUSERINFO](state,obj){
    // for(let key in obj){
    //   state.userInfo[key] = obj[key];  
    // }  
    state.userInfo = Object.assign({},state.userInfo,obj);
  },
  [types.SETNOWSONG](state,obj){
    state.musicList[state.musicPlace] = obj
  },
  [types.SENDVOLUME](state,val){
    state.volume = val
    state.dom.volume = val*0.1
  },
  [types.SENDSINGERPOSTER](state,obj){
    state.singerPoster.url = obj.url;
    state.singerPoster.singer = obj.singer;
  },
  [types.SENDMANAGESTATE](state,val){
    state.manageState = val;
  },
  [types.CHANGEBOTTOMBAR](state,val){
    state.bottomBarState = val;
  },
  [types.CHANGEPAGEGRADATION](state,val){
    state.pageGradation = val;
  },
}
